package atguigu.gulimall.seckill.controller;

import atguigu.gulimall.seckill.service.SeckillService;
import atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import com.atguigu.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;


    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> skus = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(skus);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSeckillSkuItemInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo redisTo = seckillService.getSeckillSkuItemInfo(skuId);
        return R.ok().setData(redisTo);
    }

    @GetMapping("/kill")
    @ResponseBody
    public R secKill(@RequestParam("killId")String killId,
                     @RequestParam("key")String key,
                     @RequestParam("num")Integer num){
        //1、判断是否登录,已经又拦截器验证
        //2、验证随机码,幂等性验证等
        String orderSn = seckillService.createSeckillOrderSn(killId,key,num);
        return R.ok().setData(orderSn);
    }
}
